import hashlib
import base64
import rsa
import os

def md5_encode(data):

    #把data转化为utf-8的编码格式
    data = str(data).encode('utf-8')
    print(f'data:{data}')

    #md5加密

    md5_value = hashlib.md5(data).hexdigest()
    print(md5_value)
    return md5_value.upper()

md5_encode('caipeiyan')

def base64_encode(data):
    data = str(data).encode('utf-8')

    base64_value = base64.b64encode(data).decode(encoding='utf-8')
    print(base64_value)
    return base64_value

base64_encode('caipeiyan1222')

def create_key():
    public_k,private_k = rsa.newkeys(1024)
    print(private_k)
    print(public_k)
    print(type(private_k))

    # 写入公钥到文件
    with open('./public.pem','w') as f:
        
        f.write(public_k.save_pkcs1().decode())
    with open('./private.pem','w') as f:
        
        f.write(private_k.save_pkcs1().decode())

# 使用公钥加密
def rsa_encode(data):
    with open('./public.pem') as f:
        p_k = f.read().encode()
        # print(p_k)
        pubkey = rsa.PublicKey.load_pkcs1(p_k)
        # print(pubkey)
        
    data = str(data).encode()

    # 加密
    byte_value = rsa.encrypt(data,pubkey)
    print(byte_value)
    print(data)

    # 把字节转化成字符串格式
    rsa_value = base64.b64encode(byte_value).decode('utf-8')

    print(rsa_value)
    # with open('./public.pem','rb') as f1:
    #     p_k1 = f1.read()
    #     print(p_k1)
    #     pubkey1 = rsa.PrivateKey.load_pkcs1(p_k1)
    #     print(pubkey1)
    # print(p_k == p_k1)


# create_key()

rsa_encode(1)
